package io.sage.modules.goods.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.sage.common.utils.DateUtils;
import io.sage.entity.UserEntity;
import io.sage.modules.goods.entity.BGoodsRecord;
import io.sage.modules.goods.enums.GoodsRecordStatusEnum;
import io.sage.modules.goods.enums.GoodsStatusEnum;
import io.sage.modules.goods.service.BGoodsRecordService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.sage.annotation.Login;
import io.sage.annotation.LoginUser;
import io.sage.common.utils.R;
import io.sage.modules.employer.entity.Employer;
import io.sage.modules.goods.entity.BGoods;
import io.sage.modules.goods.service.BGoodsService;
import io.sage.modules.outwarehouse.entity.BGoodsReceiveRecord;
import io.sage.modules.outwarehouse.service.BGoodsReceiveRecordService;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 *
 *
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2019-07-09 13:19:37
 */
@RestController
@RequestMapping("sage/bgoods")
public class BGoodsController {

	@Autowired
	private BGoodsService bGoodsService;

	@Autowired
	private BGoodsReceiveRecordService goodsReceiveRecordService;

	@Autowired
	private BGoodsRecordService goodsRecordService;
	/**
	 * 获取所有物品列表
	 * @param jsonObject
	 * @return
	 */
	@RequestMapping("/list")
	@ApiOperation("查询所有的物品")
	@Login
	public R list(@RequestBody JSONObject jsonObject){

		Integer pageSize = jsonObject.getInteger("pageSize");
		Integer pageNum = jsonObject.getInteger("pageNum");
		//工具的名称
		String goodsName = jsonObject.getString("goodsName");
		//工具的型号
		String goodsModel = jsonObject.getString("goodsModel");

		String goodsLike = jsonObject.getString("goodsLike");

		if(pageSize == null || pageNum == null){
			return R.error("参数不能为空");
		}

		HashMap<String, Object> paramMap = new HashMap<>();
		if(!StringUtils.isEmpty(goodsLike)){
			paramMap.put("goodsLike", goodsLike);
		}
		if(!StringUtils.isEmpty(goodsName)){
			paramMap.put("goodsName", goodsName);
		}
		if(!StringUtils.isEmpty(goodsModel)){
			paramMap.put("goodsModel", goodsModel);
		}
		//查询状态为0(正常) 和3 （待检）
		//paramMap.put("goodsStatusAll", -1);
		paramMap.put("pageNum", pageNum);
		paramMap.put("pageSize", pageSize);

		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小

		List<BGoods> goodsList = bGoodsService.selectGoodsList(paramMap);

		PageInfo<BGoods> noticeList = new PageInfo<BGoods>(goodsList);// 传入list就可以了

		return R.ok().put("goodsList", noticeList);
	}

	/**
	 * 根据id，查询物品详情
	 * @param
	 * @return
	 */
	@PostMapping("/info")
	@ApiOperation("根据id查询物品详情")
	@Login
	public R info(@RequestBody JSONObject jsonObject){
		Integer id = jsonObject.getInteger("id");
		if(id==null){
			return R.error("参数不正确");
		}
		BGoods goods = bGoodsService.selectById(id);

		return R.ok().put("goods", goods);
	}

	/**
	 * 申请工具接口
	 * @param student
	 * @param jsonObject
	 * @return
	 */
	@Login
	@PostMapping("applyGoods")
	@ApiOperation("申请工具")
	public R applyGoods(@ApiIgnore @LoginUser Employer student , @RequestBody JSONObject jsonObject){
		//待申请工具的id
		Integer id = jsonObject.getInteger("id");
		Integer employeeId = student.getId();
		if(id==null){
			return R.error("参数不正确");
		}
		//① 先查询物品是否存在，只有物品的状态是0正常，才可以申请
		BGoods goods = bGoodsService.selectById(id);
		Integer goodsStatus = goods.getGoodsStatus();
		if(goodsStatus!=0){
			return R.error("此工具不可申请");
		}
		//②修改物品的状态
		int update = bGoodsService.applyGoods(id,employeeId);//全部更新
		//③员工申请添加记录
		BGoodsReceiveRecord goodsReceiveRecord=new BGoodsReceiveRecord();
		goodsReceiveRecord.setEmployeeId(employeeId);
		goodsReceiveRecord.setGoodsId(id);

		update = goodsReceiveRecordService.insert(goodsReceiveRecord, employeeId);

		if(update>0){
			return R.ok();
		}else {
			return R.error();
		}

	}
	/**
	 * 跟新物品的状态
	 * @param user 用户信息 管理员
	 * @param jsonObject
	 * @return
	 */
	@Login
	@PostMapping("updateGoods")
	@ApiOperation("跟新工具的状态")
	public R updateGoods(@ApiIgnore @LoginUser UserEntity user , @RequestBody JSONObject jsonObject){
		//待申请工具的id
		Integer id = jsonObject.getInteger("id");
		Integer status=jsonObject.getInteger("goodsStatus");
		Long userId = user.getUserId();
		BGoods goods = bGoodsService.selectById(id);
		goods.setDueDate(DateUtils.addDateDays(goods.getDueDate(),goods.getCheckTime()));
		goods.setGoodsStatus(status==0?GoodsStatusEnum.NORMAL.getCode():GoodsStatusEnum.BAD.getCode());
		Integer update=bGoodsService.updateByPrimaryKeySelective(goods);
		if(update>0){
			BGoodsRecord bGoodsRecord=new BGoodsRecord();
			bGoodsRecord.setCreateTime(new Date());
			bGoodsRecord.setGoodsId(goods.getId());
			bGoodsRecord.setIsDel(0);
			bGoodsRecord.setUserId(userId);
			bGoodsRecord.setStatus(status==0? GoodsRecordStatusEnum.NORMAL.getCode():GoodsRecordStatusEnum.BAD.getCode());
			bGoodsRecord.setRemark(jsonObject.getString("remark"));
			bGoodsRecord.setPic(jsonObject.getString("pic"));
			bGoodsRecord.setDamage(jsonObject.getInteger("damage"));
			bGoodsRecord.setType(jsonObject.getInteger("type"));
			bGoodsRecord=goodsRecordService.insert(bGoodsRecord);
			if(bGoodsRecord.getId()!=null){
				return R.ok();
			}

		}
		return R.error();
	}

	@RequestMapping("/listByStatus")
	@PostMapping("goods:bgoods:list")
	public R listByStatus(@RequestBody Map<String, Object> params){
		Integer pageSize = 10;
		if (params != null && params.get("limit") != null) {
			pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
		}
		Integer pageNum = 0;
		if (params != null && params.get("page") != null) {
			pageNum = java.lang.Integer.parseInt(params.get("page").toString());
		}

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		if(params.get("goodsStatus")!=null){
			if(!"".equals(params.get("goodsStatus").toString())){
				paramMap.put("goodsStatus", params.get("goodsStatus").toString());
			}
		}
		if(params.get("goodsName")!=null){

			if(!"".equals(params.get("goodsName").toString())){
				paramMap.put("goodsName", params.get("goodsName").toString());
			}
		}
		if(params.get("wareHouseId")!=null){

			if(!"".equals(params.get("wareHouseId").toString())){
				paramMap.put("wareHouseId", params.get("wareHouseId").toString());
			}
		}
		if(params.get("goodsAreaId")!=null){

			if(!"".equals(params.get("goodsAreaId").toString())){
				paramMap.put("goodsAreaId", params.get("goodsAreaId").toString());
			}
		}
		if(params.get("goodsShelveId")!=null){

			if(!"".equals(params.get("goodsShelveId").toString())){
				paramMap.put("goodsShelveId", params.get("goodsShelveId").toString());
			}
		}
		if(params.get("allocationId")!=null){

			if(!"".equals(params.get("allocationId").toString())){
				paramMap.put("allocationId", params.get("allocationId").toString());
			}
		}



		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
		List<BGoods> selectGoodsList = bGoodsService.selectGoodsList(paramMap);

		PageInfo<BGoods> pageInfo = new PageInfo<BGoods>(selectGoodsList);// 传入list就可以了

		return R.ok().put("goodsList", pageInfo);
	}

}